﻿using IntoTheVoid.Units;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntoTheVoid.Models
{
	public class INIConfigManagerModel : SingletonBase<INIConfigManagerModel>
	{

		private Dictionary<string, string> launcherInfo;

		public INIConfigManagerModel()
		{
			Dictionary<string, Dictionary<string, string>> iniData = ReadIniFile("config.ini");
			launcherInfo = iniData["launcher"];
		}

		public string getLauncherValueByKey(string key)
		{
			if (launcherInfo.Keys.Contains(key))
			{
				return launcherInfo[key];
			}else { return null; }
		}

		static Dictionary<string, Dictionary<string, string>> ReadIniFile(string filePath)
		{
			var iniData = new Dictionary<string, Dictionary<string, string>>();
			string currentSection = "";

			using (StreamReader reader = new StreamReader(filePath))
			{
				string line;
				while ((line = reader.ReadLine()) != null)
				{
					line = line.Trim();
					if (line.StartsWith("[") && line.EndsWith("]"))
					{
						currentSection = line.Substring(1, line.Length - 2);
						iniData[currentSection] = new Dictionary<string, string>();
					}
					else if (!string.IsNullOrWhiteSpace(line))
					{
						string[] keyValue = line.Split(new char[] { '=' }, 2);
						if (keyValue.Length == 2)
						{
							string key = keyValue[0].Trim();
							string value = keyValue[1].Trim();
							iniData[currentSection][key] = value;
						}
					}
				}
			}

			return iniData;
		}
	}
}
